home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Python / modsupport.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  10KB  |  480 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Module support implementation */
  33.  
  34. #include "allobjects.h"
  35. #include "import.h"
  36.  
  37. #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
  38. typedef extended va_double;
  39. #else
  40. typedef double va_double;
  41. #endif
  42.  
  43. /* initmodule4() parameters:
  44.    - name is the module name
  45.    - methods is the list of top-level functions
  46.    - doc is the documentation string
  47.    - passthrough is passed as self to functions defined in the module
  48.    - api_version is the value of PYTHON_API_VERSION at the time the
  49.      module was compiled
  50. */
  51.  
  52. static char api_version_warning[] =
  53. "WARNING: Python C API version mismatch for module %s:\n\
  54.   This Python has API version %d, module %s has version %d.\n";
  55.  
  56. object *
  57. initmodule4(name, methods, doc, passthrough, module_api_version)
  58.     char *name;
  59.     struct methodlist *methods;
  60.     char *doc;
  61.     object *passthrough;
  62.     int module_api_version;
  63. {
  64.     object *m, *d, *v;
  65.     struct methodlist *ml;
  66.     if (module_api_version != PYTHON_API_VERSION)
  67.         fprintf(stderr, api_version_warning,
  68.             name, PYTHON_API_VERSION, name, module_api_version);
  69.     if ((m = add_module(name)) == NULL) {
  70.         fprintf(stderr, "initializing module: %s\n", name);
  71.         fatal("can't create a module");
  72.     }
  73.     d = getmoduledict(m);
  74.     for (ml = methods; ml->ml_name != NULL; ml++) {
  75.         v = newmethodobject(ml, passthrough);
  76.         if (v == NULL || dictinsert(d, ml->ml_name, v) != 0) {
  77.             fprintf(stderr, "initializing module: %s\n", name);
  78.             fatal("can't initialize module");
  79.         }
  80.         DECREF(v);
  81.     }
  82.     if (doc != NULL) {
  83.         v = newstringobject(doc);
  84.         if (v == NULL || dictinsert(d, "__doc__", v) != 0)
  85.             fatal("can't add doc string");
  86.         DECREF(v);
  87.     }
  88.     return m;
  89. }
  90.  
  91.  
  92. /* Helper for mkvalue() to scan the length of a format */
  93.  
  94. static int countformat PROTO((char *format, int endchar));
  95. static int countformat(format, endchar)
  96.     char *format;
  97.     int endchar;
  98. {
  99.     int count = 0;
  100.     int level = 0;
  101.     while (level > 0 || *format != endchar) {
  102.         switch (*format) {
  103.         case '\0':
  104.             /* Premature end */
  105.             err_setstr(SystemError, "unmatched paren in format");
  106.             return -1;
  107.         case '(':
  108.         case '[':
  109.         case '{':
  110.             if (level == 0)
  111.                 count++;
  112.             level++;
  113.             break;
  114.         case ')':
  115.         case ']':
  116.         case '}':
  117.             level--;
  118.             break;
  119.         case '#':
  120.         case '&':
  121.         case ',':
  122.         case ':':
  123.         case ' ':
  124.         case '\t':
  125.             break;
  126.         default:
  127.             if (level == 0)
  128.                 count++;
  129.         }
  130.         format++;
  131.     }
  132.     return count;
  133. }
  134.  
  135.  
  136. /* Generic function to create a value -- the inverse of getargs() */
  137. /* After an original idea and first implementation by Steven Miale */
  138.  
  139. static object *do_mktuple PROTO((char**, va_list *, int, int));
  140. static object *do_mklist PROTO((char**, va_list *, int, int));
  141. static object *do_mkdict PROTO((char**, va_list *, int, int));
  142. static object *do_mkvalue PROTO((char**, va_list *));
  143.  
  144.  
  145. static object *
  146. do_mkdict(p_format, p_va, endchar, n)
  147.     char **p_format;
  148.     va_list *p_va;
  149.     int endchar;
  150.     int n;
  151. {
  152.     object *d;
  153.     int i;
  154.     if (n < 0)
  155.         return NULL;
  156.     if ((d = newdictobject()) == NULL)
  157.         return NULL;
  158.     for (i = 0; i < n; i+= 2) {
  159.         object *k, *v;
  160.         k = do_mkvalue(p_format, p_va);
  161.         if (k == NULL) {
  162.             DECREF(d);
  163.             return NULL;
  164.         }
  165.         v = do_mkvalue(p_format, p_va);
  166.         if (v == NULL) {
  167.             DECREF(k);
  168.             DECREF(d);
  169.             return NULL;
  170.         }
  171.         if (dict2insert(d, k, v) < 0) {
  172.             DECREF(k);
  173.             DECREF(v);
  174.             DECREF(d);
  175.             return NULL;
  176.         }
  177.     }
  178.     if (d != NULL && **p_format != endchar) {
  179.         DECREF(d);
  180.         d = NULL;
  181.         err_setstr(SystemError, "Unmatched paren in format");
  182.     }
  183.     else if (endchar)
  184.         ++*p_format;
  185.     return d;
  186. }
  187.  
  188. static object *
  189. do_mklist(p_format, p_va, endchar, n)
  190.     char **p_format;
  191.     va_list *p_va;
  192.     int endchar;
  193.     int n;
  194. {
  195.     object *v;
  196.     int i;
  197.     if (n < 0)
  198.         return NULL;
  199.     if ((v = newlistobject(n)) == NULL)
  200.         return NULL;
  201.     for (i = 0; i < n; i++) {
  202.         object *w = do_mkvalue(p_format, p_va);
  203.         if (w == NULL) {
  204.             DECREF(v);
  205.             return NULL;
  206.         }
  207.         setlistitem(v, i, w);
  208.     }
  209.     if (v != NULL && **p_format != endchar) {
  210.         DECREF(v);
  211.         v = NULL;
  212.         err_setstr(SystemError, "Unmatched paren in format");
  213.     }
  214.     else if (endchar)
  215.         ++*p_format;
  216.     return v;
  217. }
  218.  
  219. static object *
  220. do_mktuple(p_format, p_va, endchar, n)
  221.     char **p_format;
  222.     va_list *p_va;
  223.     int endchar;
  224.     int n;
  225. {
  226.     object *v;
  227.     int i;
  228.     if (n < 0)
  229.         return NULL;
  230.     if ((v = newtupleobject(n)) == NULL)
  231.         return NULL;
  232.     for (i = 0; i < n; i++) {
  233.         object *w = do_mkvalue(p_format, p_va);
  234.         if (w == NULL) {
  235.             DECREF(v);
  236.             return NULL;
  237.         }
  238.         settupleitem(v, i, w);
  239.     }
  240.     if (v != NULL && **p_format != endchar) {
  241.         DECREF(v);
  242.         v = NULL;
  243.         err_setstr(SystemError, "Unmatched paren in format");
  244.     }
  245.     else if (endchar)
  246.         ++*p_format;
  247.     return v;
  248. }
  249.  
  250. static object *
  251. do_mkvalue(p_format, p_va)
  252.     char **p_format;
  253.     va_list *p_va;
  254. {
  255.     for (;;) {
  256.         switch (*(*p_format)++) {
  257.         case '(':
  258.             return do_mktuple(p_format, p_va, ')',
  259.                       countformat(*p_format, ')'));
  260.  
  261.         case '[':
  262.             return do_mklist(p_format, p_va, ']',
  263.                      countformat(*p_format, ']'));
  264.  
  265.         case '{':
  266.             return do_mkdict(p_format, p_va, '}',
  267.                      countformat(*p_format, '}'));
  268.  
  269.         case 'b':
  270.         case 'h':
  271.         case 'i':
  272.             return newintobject((long)va_arg(*p_va, int));
  273.  
  274.         case 'l':
  275.             return newintobject((long)va_arg(*p_va, long));
  276.  
  277.         case 'f':
  278.         case 'd':
  279.             return newfloatobject((double)va_arg(*p_va, va_double));
  280.  
  281.         case 'c':
  282.         {
  283.             char p[1];
  284.             p[0] = va_arg(*p_va, int);
  285.             return newsizedstringobject(p, 1);
  286.         }
  287.  
  288.         case 's':
  289.         case 'z':
  290.         {
  291.             object *v;
  292.             char *str = va_arg(*p_va, char *);
  293.             int n;
  294.             if (**p_format == '#') {
  295.                 ++*p_format;
  296.                 n = va_arg(*p_va, int);
  297.             }
  298.             else
  299.                 n = -1;
  300.             if (str == NULL) {
  301.                 v = None;
  302.                 INCREF(v);
  303.             }
  304.             else {
  305.                 if (n < 0)
  306.                     n = strlen(str);
  307.                 v = newsizedstringobject(str, n);
  308.             }
  309.             return v;
  310.         }
  311.  
  312.         case 'S':
  313.         case 'O':
  314.         if (**p_format == '&') {
  315.             typedef object *(*converter) PROTO((void *));
  316.             converter func = va_arg(*p_va, converter);
  317.             void *arg = va_arg(*p_va, void *);
  318.             ++*p_format;
  319.             return (*func)(arg);
  320.         }
  321.         else {
  322.             object *v;
  323.             v = va_arg(*p_va, object *);
  324.             if (v != NULL)
  325.                 INCREF(v);
  326.             else if (!err_occurred())
  327.                 /* If a NULL was passed
  328.                  * because a call that should
  329.                  * have constructed a value
  330.                  * failed, that's OK, and we
  331.                  * pass the error on; but if
  332.                  * no error occurred it's not
  333.                  * clear that the caller knew
  334.                  * what she was doing. */
  335.                 err_setstr(SystemError,
  336.                        "NULL object passed to mkvalue");
  337.             return v;
  338.         }
  339.  
  340.         case ':':
  341.         case ',':
  342.         case ' ':
  343.         case '\t':
  344.             break;
  345.  
  346.         default:
  347.             err_setstr(SystemError,
  348.                    "bad format char passed to mkvalue");
  349.             return NULL;
  350.  
  351.         }
  352.     }
  353. }
  354.  
  355.  
  356. #ifdef HAVE_STDARG_PROTOTYPES
  357. /* VARARGS 2 */
  358. object *mkvalue(char *format, ...)
  359. #else
  360. /* VARARGS */
  361. object *mkvalue(va_alist) va_dcl
  362. #endif
  363. {
  364.     va_list va;
  365.     object* retval;
  366. #ifdef HAVE_STDARG_PROTOTYPES
  367.     va_start(va, format);
  368. #else
  369.     char *format;
  370.     va_start(va);
  371.     format = va_arg(va, char *);
  372. #endif
  373.     retval = vmkvalue(format, va);
  374.     va_end(va);
  375.     return retval;
  376. }
  377.  
  378. object *
  379. vmkvalue(format, va)
  380.     char *format;
  381.     va_list va;
  382. {
  383.     char *f = format;
  384.     int n = countformat(f, '\0');
  385.     va_list lva;
  386.  
  387. #ifdef VA_LIST_IS_ARRAY
  388.     memcpy(lva, va, sizeof(va_list));
  389. #else
  390.     lva = va;
  391. #endif
  392.  
  393.     if (n < 0)
  394.         return NULL;
  395.     if (n == 0) {
  396.         INCREF(None);
  397.         return None;
  398.     }
  399.     if (n == 1)
  400.         return do_mkvalue(&f, &lva);
  401.     return do_mktuple(&f, &lva, '\0', n);
  402. }
  403.  
  404.  
  405. #ifdef HAVE_STDARG_PROTOTYPES
  406. object *
  407. PyEval_CallFunction(object *obj, char *format, ...)
  408. #else
  409. object *
  410. PyEval_CallFunction(obj, format, va_alist)
  411.     object *obj;
  412.     char *format;
  413.     va_dcl
  414. #endif
  415. {
  416.     va_list vargs;
  417.     object *args;
  418.     object *res;
  419.  
  420. #ifdef HAVE_STDARG_PROTOTYPES
  421.     va_start(vargs, format);
  422. #else
  423.     va_start(vargs);
  424. #endif
  425.  
  426.     args = vmkvalue(format, vargs);
  427.     va_end(vargs);
  428.  
  429.     if (args == NULL)
  430.         return NULL;
  431.  
  432.     res = call_object(obj, args);
  433.     DECREF(args);
  434.  
  435.     return res;
  436. }
  437.  
  438.  
  439. #ifdef HAVE_STDARG_PROTOTYPES
  440. object *
  441. PyEval_CallMethod(object *obj, char *methonname, char *format, ...)
  442. #else
  443. object *
  444. PyEval_CallMethod(obj, methonname, format, va_alist)
  445.     object *obj;
  446.     char *methonname;
  447.     char *format;
  448.     va_dcl
  449. #endif
  450. {
  451.     va_list vargs;
  452.     object *meth;
  453.     object *args;
  454.     object *res;
  455.  
  456.     meth = getattr(obj, methonname);
  457.     if (meth == NULL)
  458.         return NULL;
  459.  
  460. #ifdef HAVE_STDARG_PROTOTYPES
  461.     va_start(vargs, format);
  462. #else
  463.     va_start(vargs);
  464. #endif
  465.  
  466.     args = vmkvalue(format, vargs);
  467.     va_end(vargs);
  468.  
  469.     if (args == NULL) {
  470.         DECREF(meth);
  471.         return NULL;
  472.     }
  473.  
  474.     res = call_object(meth, args);
  475.     DECREF(meth);
  476.     DECREF(args);
  477.  
  478.     return res;
  479. }
  480.